package com.minio;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Slf4j
public class ImageMerger {

    public static void main(String[] args) throws IOException {
        Gson gson = new Gson();

        try (FileReader reader = new FileReader("/Users/sangcixiang/Desktop/project/yehuo/api/upload_file/comfyWork.json")) {
            // 定义 Map 类型（通过 TypeToken 保留泛型信息）
            Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
            Map<String, Object> jsonMap = gson.fromJson(reader, mapType);
            String json = JSONObject.toJSONString(jsonMap);

            log.info(json);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }



    private void test() throws IOException {

    }

    private static void crop() throws IOException {
        // 2. 定义裁剪区域（根据实际需求调整参数）
        int x = 90;      // 左上角X坐标
        int y = 12;       // 左上角Y坐标
        int width = 500;  // 裁剪宽度
        int height = 500; // 裁剪高度

        String targetPath = "/Users/sangcixiang/Desktop/测试图片/photo_test/0.png";
        String toPath = "/Users/sangcixiang/Desktop/测试图片/photo_test/"+new Date().getTime() + ".png";
        BufferedImage originalImage = ImageIO.read(new File(targetPath));

        // 水平反转
        BufferedImage horizontalFlip = flipHorizontal(originalImage);

        // 保存结果
        ImageIO.write(horizontalFlip, "png", new File(toPath));

//        BufferedImage croppedImage = new BufferedImage(width, height, originalImage.getType());
//        croppedImage.getGraphics().drawImage(originalImage.getSubimage(x, y, width, height), 0, 0, null);
//
//
//        ImageIO.write(croppedImage, "png", new File(toPath));
    }

    /**
     * 水平反转图片（左右翻转）
     */
    public static BufferedImage flipHorizontal(BufferedImage image) {
        int width = image.getWidth();
        int height = image.getHeight();
        BufferedImage flipped = new BufferedImage(width, height, image.getType());
        for (int y = 0; y < height; y++) {  // 遍历每一行
            for (int x = 0; x < width; x++) {  // 遍历每一列
                // 原始像素位置 (x, y)，反转后位置 (width - 1 - x, y)
                int pixel = image.getRGB(x, y);
                flipped.setRGB(width - 1 - x, y, pixel);
            }
        }
        return flipped;
    }

}
